﻿using System;
using System.Web.Mvc;
using PagedList;
using Rotativa;
using Rotativa.Options;

using App.Interface;

namespace SiteCredivale.Areas.Intranet.Controllers
{
    public class UsuariosController : Controller
    {
        //
        // GET: /Intranet/Usuarios/
       

        private readonly IUsuarioAppService _usuarioapp;
        


        public UsuariosController(IUsuarioAppService usuarioapp)
        {
            _usuarioapp = usuarioapp;
         
        }

        public UsuariosController()
        
        {

     

         }


        [Authorize]
        public ActionResult Lista(int ? page,string tipo,string set_uni,string nome)
        {
            try
            {

        
            if (tipo == "qual" || tipo == null)
            {
                tipo = "qual";
                set_uni = "0";
            }
            ViewBag.tipo = tipo;
            ViewBag.set_uni = set_uni;
            ViewBag.nome = nome;

           
            const int pageSize = 15;
            var pagedavisos = _usuarioapp.Listar(tipo,int.Parse(set_uni),nome ?? "").ToPagedList(page ?? 1, pageSize);
            return View(pagedavisos);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        [Authorize,HttpPost]
        public ActionResult Lista(FormCollection form)
        {

            try
            {

         
            if (form["tipo"].ToString() == "qual" || form["tipo"].ToString() == null)
            {
                form["tipo"] = "qual";
                form["set_uni"] = "0";
            }

            ViewBag.tipo = form["tipo"].ToString();
            ViewBag.set_uni = form["set_uni"].ToString();
            ViewBag.nome = form["nome"].ToString();

     
            const int pageSize = 15;
            var pagedavisos = _usuarioapp.Listar(form["tipo"].ToString(), int.Parse(form["set_uni"].ToString()), form["nome"].ToString() ?? "").ToPagedList(1, pageSize);
            return View(pagedavisos);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        [HttpGet]
        public ActionResult GetTipo()
        {


            try
            {



                return Json(_usuarioapp.busca_setor_unidade(Request.QueryString.Get("tipo"),-1), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                ViewBag.res = "Erro ao recuperar o filtro: " + ex.Message + ",contate o administrador";
                throw ex;
            }

          

        } 
         [Authorize]
         public ActionResult Imprimir(string tipo, string set_uni, string nome)
          {
              try
              {
                     if (tipo == "qual" || tipo == null)
            {
                tipo = "qual";
                set_uni = "0";
            }
                     

                    
                     var lista = _usuarioapp.Listar(tipo, int.Parse(set_uni), nome ?? "");
                     var pdf = new ViewAsPdf
                     {
                         ViewName = "relusuarios",
                         Model = lista,
                         PageSize = Size.A4,
                         PageMargins = new Margins { Bottom = 5, Left = 5, Right = 5, Top = 5},
                         PageOrientation = Orientation.Landscape
                     };

                     return pdf;


              }
              catch (Exception ex)
              {
                  
                  throw ex;
              }


           

          }
            

    }
}
